package com.sany.assess.result;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sany.assess.constants.SysConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author vincent
 */
public class ActionUtils {

    private final static Logger logger = LoggerFactory.getLogger(ActionUtils.class);

    public static JsonActionResult handleResult(HttpServletResponse response) {
        JsonActionResult result = new JsonActionResult();
        result.setSuccess(true);
        return result;
    }

    public static JsonActionResult handleResult(boolean success, String msg) {
        JsonActionResult result = new JsonActionResult();
        result.setMessage(msg);
        result.setSuccess(success);
        return result;
    }

    public static JsonActionResult handleResult(Object data) {
        JsonActionResult result = new JsonActionResult();
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    private static void setHeader(HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-store, max-age=0, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
    }

    public static void handleResult(HttpServletResponse response, JSONObject jsonObject) {
        setHeader(response);
        try {
            PrintWriter out = response.getWriter();
            if (!jsonObject.containsKey(SysConstant.REQUEST_SUCCESS_KEY)) {
                jsonObject.put(SysConstant.REQUEST_SUCCESS_KEY, true);
            }
            out.write(jsonObject.toString());
            out.close();
        } catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
    }

    public static void handleResult(HttpServletResponse response, Object object) {
        setHeader(response);
        try {
            PrintWriter out = response.getWriter();
            out.write(object.toString());
            out.close();
        } catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
    }

    public static JsonActionResult handleListResult(HttpServletResponse response, IPage<?> pageResult) {
        JqueryActionResult result = new JqueryActionResult();
        result.setSuccess(true);
        result.setData(pageResult.getRecords());
        //result.setRows(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setCurrentPage(pageResult.getCurrent());
        result.setTotalPage(pageResult.getPages());
        return result;
    }


    /**
     * <b>Method Function Description:</b><br>
     * 判断是否为Ajax请求
     *
     * @param request
     * @return <br>
     * <b>Method Logic Description:</b>
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        if (requestType != null && requestType.equals("XMLHttpRequest")) {
            return true;
        } else if (StringUtils.equals("XMLHttpRequest", request.getParameter("X-Requested-With"))) {
            return true;
        } else {
            return false;
        }
    }


}
